68template <
typename Derived>
69inline Eigen::Matrix<typename Derived::Scalar, 3, 3>
skew(
const Eigen::MatrixBase<Derived>& _v)
73 typedef typename Derived::Scalar T;
75 Eigen::Matrix<T, 3, 3> sk;
77 sk << (T)0.0, -_v(2), +_v(1), +_v(2), (T)0.0, -_v(0), -_v(1), +_v(0), (T)0.0;
87template <
typename Derived>
88inline Eigen::Matrix<typename Derived::Scalar, 3, 1>
vee(
const Eigen::MatrixBase<Derived>& _m)
92 typedef typename Derived::Scalar T;
94 return (Eigen::Matrix<T, 3, 1>() << _m(2, 1), _m(0, 2), _m(1, 0)).finished();
107template <
typename Derived>
108inline Eigen::Matrix<typename Derived::Scalar, 3, 3>
q2R(
const Eigen::QuaternionBase<Derived>& _q)
118template <
typename Derived>
119inline Eigen::Matrix<typename Derived::Scalar, 3, 3>
q2R(
const Eigen::MatrixBase<Derived>& _q)
122 Eigen::Quaternion<typename Derived::Scalar> q(_q(3), _q(0), _q(1), _q(2));
131template <
typename Derived>
132inline Eigen::Quaternion<typename Derived::Scalar>
R2q(
const Eigen::MatrixBase<Derived>& _R)
136 return Eigen::Quaternion<typename Derived::Scalar>(_R);
144inline Eigen::Quaternion<typename D::Scalar>
e2q(
const Eigen::MatrixBase<D>& _euler)
148 typedef typename D::Scalar T;
150 const Eigen::AngleAxis<T> ax = Eigen::AngleAxis<T>(_euler(0), Eigen::Matrix<T, 3, 1>::UnitX());
151 const Eigen::AngleAxis<T> ay = Eigen::AngleAxis<T>(_euler(1), Eigen::Matrix<T, 3, 1>::UnitY());
152 const Eigen::AngleAxis<T> az = Eigen::AngleAxis<T>(_euler(2), Eigen::Matrix<T, 3, 1>::UnitZ());
154 return Eigen::Quaternion<T>(az * ay * ax);
161template <
typename Derived>
162inline Eigen::Matrix<typename Derived::Scalar, 3, 3>
e2R(
const Eigen::MatrixBase<Derived>& _e)
166 return e2q(_e).matrix();
171template <
typename Derived>
172inline typename Eigen::Matrix<typename Derived::Scalar, 3, 1>
R2e(
const Eigen::MatrixBase<Derived>& _R)
174 Eigen::Matrix<typename Derived::Scalar, 3, 1> e;
176 e(0) =
atan2(_R(2, 1), _R(2, 2));
177 e(1) =
atan2(-_R(2, 0),
sqrt(_R(0, 0) * _R(0, 0) + _R(1, 0) * _R(1, 0)));
178 e(2) =
atan2(_R(1, 0), _R(0, 0));
185template <
typename Derived>
186inline typename Eigen::Matrix<typename Derived::Scalar, 3, 1>
q2e(
const Eigen::MatrixBase<Derived>& _q)
188 typedef typename Derived::Scalar T;
189 Eigen::Matrix<T, 3, 1> e;
191 const double& w = _q(3);
192 const double& x = _q(0);
193 const double& y = _q(1);
194 const double& z = _q(2);
206 double r00 = T(1) - T(2) * (yy + zz);
207 double r10 = T(2) * (xy + wz);
208 double r20 = T(2) * (xz - wy);
209 double r21 = T(2) * (yz + wx);
210 double r22 = T(1) - T(2) * (xx + yy);
212 e(0) =
atan2(r21, r22);
213 e(1) =
atan2(-r20,
sqrt(r00 * r00 + r10 * r10));
214 e(2) =
atan2(r10, r00);
221template <
typename Derived>
222inline typename Eigen::Matrix<typename Derived::Scalar, 3, 1>
q2e(
const Eigen::QuaternionBase<Derived>& _q)
224 return q2e(_q.coeffs());
229template <
typename Derived>
230inline typename Eigen::MatrixBase<Derived>::Scalar
getYaw(
const Eigen::MatrixBase<Derived>& _M)
233 if (_M.rows() == 3 and _M.cols() == 3)
return std::atan2(_M(1, 0), _M(0, 0));
236 if (_M.rows() == 4 and _M.cols() == 1)
238 typedef typename Derived::Scalar T;
239 const double& w = _M(3, 0);
240 const double& x = _M(0, 0);
241 const double& y = _M(1, 0);
242 const double& z = _M(2, 0);
243 return atan2(T(2) * (x * y + w * z), T(1) - T(2) * (y * y + z * z));
247 assert(
false &&
"getYaw wrong input size. Should be 3x3 matrix (rotation matrix) or 4d vector (quaternion).");
252template <
typename Derived>
253inline typename Eigen::QuaternionBase<Derived>::Scalar
getYaw(
const Eigen::QuaternionBase<Derived>& _q)
255 return getYaw(_q.coeffs());
266template <
typename Derived>
267inline Eigen::Quaternion<typename Derived::Scalar>
exp_q(
const Eigen::MatrixBase<Derived>& _v)
275 typedef typename Derived::Scalar T;
277 T angle_squared = _v.squaredNorm();
278 T angle =
sqrt(angle_squared);
282 return Eigen::Quaternion<T>(Eigen::AngleAxis<T>(angle, _v.normalized()));
286 return Eigen::Quaternion<T>((T)1.0, _v(0, 0) / (T)2, _v(1, 0) / (T)2, _v(2, 0) / (T)2);
295template <
typename Derived>
296inline Eigen::Matrix<typename Derived::Scalar, 3, 1>
log_q(
const Eigen::QuaternionBase<Derived>& _q)
306 typedef typename Derived::Scalar T;
308 Eigen::Matrix<T, 3, 1> vec = _q.vec();
309 const T sin_angle_squared = vec.squaredNorm();
312 const T sin_angle =
sqrt(sin_angle_squared);
313 const T& cos_angle = _q.w();
325 T(2.0) * ((cos_angle < T(0.0)) ?
atan2(-sin_angle, -cos_angle) :
atan2(sin_angle, cos_angle));
326 const T k = two_angle / sin_angle;
341template <
typename Derived>
342inline Eigen::Matrix<typename Derived::Scalar, 3, 3>
exp_R(
const Eigen::MatrixBase<Derived>& _v)
348 typedef typename Derived::Scalar T;
350 T angle_squared = _v.squaredNorm();
351 T angle =
sqrt(angle_squared);
354 return Eigen::AngleAxis<T>(angle, _v.normalized()).toRotationMatrix();
356 return Eigen::Matrix<T, 3, 3>::Identity() +
skew(_v);
364template <
typename Derived>
365inline Eigen::Matrix<typename Derived::Scalar, 3, 1>
log_R(
const Eigen::MatrixBase<Derived>& _R)
375template <
typename Derived>
376inline Eigen::Quaternion<typename Derived::Scalar>
v2q(
const Eigen::MatrixBase<Derived>& _v)
386template <
typename Derived>
387inline Eigen::Matrix<typename Derived::Scalar, 3, 1>
q2v(
const Eigen::QuaternionBase<Derived>& _q)
397template <
typename Derived>
398inline Eigen::Matrix<typename Derived::Scalar, 3, 3>
v2R(
const Eigen::MatrixBase<Derived>& _v)
408template <
typename Derived>
409inline Eigen::Matrix<typename Derived::Scalar, 3, 1>
R2v(
const Eigen::MatrixBase<Derived>& _R)
431template <
typename Derived>
432inline Eigen::Matrix<typename Derived::Scalar, 3, 3>
jac_SO3_right(
const Eigen::MatrixBase<Derived>& _theta)
440 typedef typename Derived::Scalar T;
442 T theta2 = _theta.squaredNorm();
443 Eigen::Matrix<T, 3, 3> W(
skew(_theta));
445 return Eigen::Matrix<T, 3, 3>::Identity() - (T)0.5 * W;
446 T theta =
sqrt(theta2);
447 Eigen::Matrix<T, 3, 3> M1, M2;
448 M1.noalias() = ((T)1 -
cos(theta)) / theta2 * W;
449 M2.noalias() = (theta -
sin(theta)) / (theta2 * theta) * (W * W);
450 return Eigen::Matrix<T, 3, 3>::Identity() - M1 + M2;
471template <
typename Derived>
472inline Eigen::Matrix<typename Derived::Scalar, 3, 3>
jac_SO3_right_inv(
const Eigen::MatrixBase<Derived>& _theta)
480 typedef typename Derived::Scalar T;
482 T theta2 = _theta.squaredNorm();
483 Eigen::Matrix<T, 3, 3> W(
skew(_theta));
485 return Eigen::Matrix<T, 3, 3>::Identity() + (T)0.5 * W;
486 T theta =
sqrt(theta2);
487 Eigen::Matrix<T, 3, 3> M;
488 M.noalias() = ((T)1.0 / theta2 - ((T)1.0 +
cos(theta)) / ((T)2.0 * theta *
sin(theta))) * (W * W);
489 return Eigen::Matrix<T, 3, 3>::Identity() + (T)0.5 * W + M;
505template <
typename Derived>
506inline Eigen::Matrix<typename Derived::Scalar, 3, 3>
jac_SO3_left(
const Eigen::MatrixBase<Derived>& _theta)
514 typedef typename Derived::Scalar T;
516 T theta2 = _theta.squaredNorm();
517 Eigen::Matrix<T, 3, 3> W(
skew(_theta));
519 return Eigen::Matrix<T, 3, 3>::Identity() - (T)0.5 * W;
520 T theta =
sqrt(theta2);
521 Eigen::Matrix<T, 3, 3> M1, M2;
522 M1.noalias() = ((T)1 -
cos(theta)) / theta2 * W;
523 M2.noalias() = (theta -
sin(theta)) / (theta2 * theta) * (W * W);
524 return Eigen::Matrix<T, 3, 3>::Identity() + M1 + M2;
544template <
typename Derived>
545inline Eigen::Matrix<typename Derived::Scalar, 3, 3>
jac_SO3_left_inv(
const Eigen::MatrixBase<Derived>& _theta)
553 typedef typename Derived::Scalar T;
555 T theta2 = _theta.squaredNorm();
556 Eigen::Matrix<T, 3, 3> W(
skew(_theta));
558 return Eigen::Matrix<T, 3, 3>::Identity() + (T)0.5 * W;
559 T theta =
sqrt(theta2);
560 Eigen::Matrix<T, 3, 3> M;
561 M.noalias() = ((T)1.0 / theta2 - ((T)1.0 +
cos(theta)) / ((T)2.0 * theta *
sin(theta))) * (W * W);
562 return Eigen::Matrix<T, 3, 3>::Identity() - (T)0.5 * W + M;
567template <
typename D1,
typename D2,
typename D3,
typename D4,
typename D5>
568inline void compose(
const Eigen::QuaternionBase<D1>& _q1,
569 const Eigen::QuaternionBase<D2>& _q2,
570 Eigen::QuaternionBase<D3>& _q_comp,
571 Eigen::MatrixBase<D4>& _J_comp_q1,
572 Eigen::MatrixBase<D5>& _J_comp_q2)
579 _J_comp_q1 =
q2R(_q2.conjugate());
580 _J_comp_q2.setIdentity();
585template <
typename D1,
typename D2,
typename D3,
typename D4,
typename D5>
586inline void between(
const Eigen::QuaternionBase<D1>& _q1,
587 const Eigen::QuaternionBase<D2>& _q2,
588 Eigen::QuaternionBase<D3>& _q_between,
589 Eigen::MatrixBase<D4>& _J_between_q1,
590 Eigen::MatrixBase<D5>& _J_between_q2)
595 _q_between = _q1.conjugate() * _q2;
597 _J_between_q1 = -
q2R(_q2.conjugate() * _q1);
598 _J_between_q2.setIdentity();
603template <
typename D1,
typename D2>
604inline Eigen::Quaternion<typename D1::Scalar>
plus_right(
const Eigen::QuaternionBase<D1>& q,
605 const Eigen::MatrixBase<D2>& v)
613template <
typename D1,
typename D2>
614inline Eigen::Matrix<typename D2::Scalar, 3, 1>
minus_right(
const Eigen::QuaternionBase<D1>& q1,
615 const Eigen::QuaternionBase<D2>& q2)
617 return log_q(q1.conjugate() * q2);
622template <
typename D1,
typename D2>
623inline Eigen::Quaternion<typename D1::Scalar>
plus_left(
const Eigen::MatrixBase<D2>& v,
624 const Eigen::QuaternionBase<D1>& q)
632template <
typename D1,
typename D2>
633inline Eigen::Matrix<typename D2::Scalar, 3, 1>
minus_left(
const Eigen::QuaternionBase<D1>& q1,
634 const Eigen::QuaternionBase<D2>& q2)
636 return log_q(q2 * q1.conjugate());
641template <
typename D1,
typename D2>
642inline Eigen::Quaternion<typename D1::Scalar>
plus(
const Eigen::QuaternionBase<D1>& q,
const Eigen::MatrixBase<D2>& v)
649template <
typename D1,
typename D2>
650inline Eigen::Matrix<typename D2::Scalar, 3, 1>
minus(
const Eigen::QuaternionBase<D1>& q1,
651 const Eigen::QuaternionBase<D2>& q2)
658template <
typename D1,
typename D2>
659inline Eigen::Matrix<typename D2::Scalar, 3, 1>
diff(
const Eigen::QuaternionBase<D1>& q1,
660 const Eigen::QuaternionBase<D2>& q2)
662 return minus(q1, q2);
const double EPS_SMALL
Definition: wolf.h:76
Jet< double, N > remainder(const Jet< double, N > &_x, long double _y)
Definition: wolf_jet.h:31
Jet< T, N > sin(const Jet< T, N > &f)
Definition: jet.h:641
Jet< T, N > atan2(const Jet< T, N > &g, const Jet< T, N > &f)
Definition: jet.h:1167
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:617
Jet< T, N > cos(const Jet< T, N > &f)
Definition: jet.h:626
Definition: capture_base.h:23
Eigen::Matrix< typename Derived::Scalar, 3, 3 > q2R(const Eigen::QuaternionBase< Derived > &_q)
Quaternion to rotation matrix conversion.
Definition: rotations.h:108
Eigen::Matrix< typename Derived::Scalar, 3, 1 > q2e(const Eigen::MatrixBase< Derived > &_q)
Euler angles (roll, pitch, yaw) of a quaternion given as a 4-vector [x, y, z, w].
Definition: rotations.h:186
Eigen::Matrix< typename D2::Scalar, 3, 1 > minus(const Eigen::QuaternionBase< D1 > &q1, const Eigen::QuaternionBase< D2 > &q2)
Minus (right minus): log_q(q1.conj * q2)
Definition: rotations.h:650
Eigen::Quaternion< typename Derived::Scalar > v2q(const Eigen::MatrixBase< Derived > &_v)
Rotation vector to quaternion conversion.
Definition: rotations.h:376
Eigen::Matrix< typename Derived::Scalar, 3, 3 > jac_SO3_right_inv(const Eigen::MatrixBase< Derived > &_theta)
Computes Jrinv (inverse of Right Jacobian which corresponds to the jacobian of log) Right Jacobian fo...
Definition: rotations.h:472
T pi2pi(const T &_angle)
Returns the angle wrapped to [-pi, pi].
Definition: rotations.h:33
Eigen::Matrix< typename Derived::Scalar, 3, 1 > R2e(const Eigen::MatrixBase< Derived > &_R)
Euler angles (roll, pitch, yaw) of a rotation matrix.
Definition: rotations.h:172
Eigen::Quaternion< typename Derived::Scalar > exp_q(const Eigen::MatrixBase< Derived > &_v)
Quaternion exponential map.
Definition: rotations.h:267
Eigen::Matrix< typename Derived::Scalar, 3, 3 > jac_SO3_left(const Eigen::MatrixBase< Derived > &_theta)
Computes Jl (Left Jacobian) Left Jacobian for exp map in SO(3) - equation (10.86) and following equat...
Definition: rotations.h:506
Eigen::Matrix< typename D2::Scalar, 3, 1 > minus_left(const Eigen::QuaternionBase< D1 > &q1, const Eigen::QuaternionBase< D2 > &q2)
Left minus: log_q(q2 * q1.conj)
Definition: rotations.h:633
Eigen::Matrix< typename D2::Scalar, 3, 1 > diff(const Eigen::QuaternionBase< D1 > &q1, const Eigen::QuaternionBase< D2 > &q2)
Difference (right minus): log_q(q1.conj * q2)
Definition: rotations.h:659
T toDeg(const T rad)
Conversion to degrees.
Definition: rotations.h:55
Eigen::Quaternion< typename D1::Scalar > plus_right(const Eigen::QuaternionBase< D1 > &q, const Eigen::MatrixBase< D2 > &v)
Right plus: q * exp_q(v)
Definition: rotations.h:604
Eigen::Matrix< typename Derived::Scalar, 3, 3 > v2R(const Eigen::MatrixBase< Derived > &_v)
Rotation vector to rotation matrix conversion.
Definition: rotations.h:398
Eigen::Matrix< typename Derived::Scalar, 3, 1 > q2v(const Eigen::QuaternionBase< Derived > &_q)
Quaternion to rotation vector conversion.
Definition: rotations.h:387
Eigen::Matrix< typename Derived::Scalar, 3, 3 > jac_SO3_right(const Eigen::MatrixBase< Derived > &_theta)
Computes Jr (Right Jacobian) Right Jacobian for exp map in SO(3) - equation (10.86) and following equ...
Definition: rotations.h:432
Eigen::Matrix< typename Derived::Scalar, 3, 3 > exp_R(const Eigen::MatrixBase< Derived > &_v)
Rotation matrix exponential map.
Definition: rotations.h:342
Eigen::Matrix< typename D2::Scalar, 3, 1 > minus_right(const Eigen::QuaternionBase< D1 > &q1, const Eigen::QuaternionBase< D2 > &q2)
Right minus: log_q(q1.conj * q2)
Definition: rotations.h:614
Eigen::Matrix< typename Derived::Scalar, 3, 1 > log_q(const Eigen::QuaternionBase< Derived > &_q)
Quaternion logarithmic map.
Definition: rotations.h:296
Eigen::Quaternion< typename D::Scalar > e2q(const Eigen::MatrixBase< D > &_euler)
Euler angles to quaternion.
Definition: rotations.h:144
T toRad(const T deg)
Conversion to radians.
Definition: rotations.h:44
Eigen::Matrix< typename Derived::Scalar, 3, 1 > R2v(const Eigen::MatrixBase< Derived > &_R)
Rotation matrix to rotation vector conversion.
Definition: rotations.h:409
Eigen::Matrix< typename Derived::Scalar, 3, 1 > log_R(const Eigen::MatrixBase< Derived > &_R)
Rotation matrix logarithmic map.
Definition: rotations.h:365
Eigen::Quaternion< typename Derived::Scalar > R2q(const Eigen::MatrixBase< Derived > &_R)
Rotation matrix to quaternion conversion.
Definition: rotations.h:132
Eigen::Quaternion< typename D1::Scalar > plus_left(const Eigen::MatrixBase< D2 > &v, const Eigen::QuaternionBase< D1 > &q)
Left plus: exp_q(v) * q.
Definition: rotations.h:623
Eigen::Matrix< typename Derived::Scalar, 3, 3 > jac_SO3_left_inv(const Eigen::MatrixBase< Derived > &_theta)
Computes Jl_inv (inverse of Left Jacobian which corresponds to the jacobian of log) Left Jacobian for...
Definition: rotations.h:545
Eigen::Quaternion< typename D1::Scalar > plus(const Eigen::QuaternionBase< D1 > &q, const Eigen::MatrixBase< D2 > &v)
Plus (right plus): q * exp_q(v)
Definition: rotations.h:642
Eigen::Matrix< typename Derived::Scalar, 3, 1 > vee(const Eigen::MatrixBase< Derived > &_m)
Inverse of skew symmetric matrix.
Definition: rotations.h:88
Eigen::Matrix< typename Derived::Scalar, 3, 3 > e2R(const Eigen::MatrixBase< Derived > &_e)
Euler angles to rotation matrix.
Definition: rotations.h:162
void between(const Eigen::QuaternionBase< D1 > &_q1, const Eigen::QuaternionBase< D2 > &_q2, Eigen::QuaternionBase< D3 > &_q_between, Eigen::MatrixBase< D4 > &_J_between_q1, Eigen::MatrixBase< D5 > &_J_between_q2)
Quaternion between two quaternions q1.conj * q2, with the Jacobians wrt local (right) perturbations.
Definition: rotations.h:586
Eigen::Matrix< typename Derived::Scalar, 3, 3 > skew(const Eigen::MatrixBase< Derived > &_v)
Skew symmetric matrix.
Definition: rotations.h:69
Eigen::MatrixBase< Derived >::Scalar getYaw(const Eigen::MatrixBase< Derived > &_M)
Yaw angle of a rotation matrix or of a quaternion given as a 4-vector [x, y, z, w].
Definition: rotations.h:230
void compose(const Eigen::QuaternionBase< D1 > &_q1, const Eigen::QuaternionBase< D2 > &_q2, Eigen::QuaternionBase< D3 > &_q_comp, Eigen::MatrixBase< D4 > &_J_comp_q1, Eigen::MatrixBase< D5 > &_J_comp_q2)
Composition of two quaternions q1 * q2, with the Jacobians wrt local (right) perturbations.
Definition: rotations.h:568
static void check(const T &t)
Asserts the matrix size dynamically or statically.
Definition: wolf.h:194
#define M_TORAD
Definition: wolf.h:68
#define M_TODEG
Definition: wolf.h:69